/*
 * Copyright 2012 Gregor Schauer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.schauer.gregor.vienna.datastore.file;

import at.schauer.gregor.vienna.context.ViennaContext;
import at.schauer.gregor.vienna.datastore.StorageMetadata;
import at.schauer.gregor.vienna.exception.ViennaException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.CanReadFileFilter;
import org.apache.commons.io.filefilter.FileFileFilter;
import org.apache.commons.io.filefilter.FileFilterUtils;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author Gregor Schauer
 */
public abstract class AbstractFileDataStore<T> implements FileDataStore<T> {
	protected ViennaContext ctx;
	protected File rootDirectory;
	protected String ext;
	protected FileFilter fileFilter;

	public AbstractFileDataStore(ViennaContext ctx, File rootDirectory, String ext) {
		this.ctx = ctx;
		this.rootDirectory = rootDirectory;
		this.setExt(ext);
	}

	@Override
	public <E extends T> E load(Class<E> type, Object identifier) {
		StorageMetadata<E> metadata = StorageMetadata.create(ctx, type);
		metadata.identifier = identifier;
		return read(resolveFile(metadata), metadata);
	}

	@Override
	public Object save(T obj) {
		StorageMetadata<T> metadata = StorageMetadata.create(ctx, obj);
		write(resolveFile(metadata), metadata);
		return metadata.identifier;
	}

	@Override
	public void delete(T obj) {
		try {
			FileUtils.forceDelete(resolveFile(StorageMetadata.create(ctx, obj)));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public <E extends T> Collection<E> list(Class<E> type) {
		StorageMetadata<E> metadata = StorageMetadata.create(ctx, type);
		File dir = resolveDirectory(metadata);
		File[] files = dir.listFiles(fileFilter);
		List<E> objects = new ArrayList<E>(files.length);
		for (File file : files) {
			metadata.identifier = FilenameUtils.getBaseName(file.getName());
			objects.add(read(file, metadata));
		}
		return objects;
	}

	@Override
	public File getRootDirectory() {
		return rootDirectory;
	}

	@Override
	public String getDirectoryName(StorageMetadata<? extends T> metadata) {
		return metadata.entityName;
	}

	@Override
	public String getFileName(StorageMetadata<? extends T> metadata) {
		return metadata.identifier != null ? String.valueOf(metadata.identifier) : null;
	}

	@Override
	public String getExtension(StorageMetadata<? extends T> metadata) {
		return ext;
	}

	protected void setExt(String ext) {
		this.ext = ext;
		this.fileFilter = FileFilterUtils.and(FileFileFilter.FILE, CanReadFileFilter.CAN_READ,
				FileFilterUtils.suffixFileFilter('.' + ext, IOCase.INSENSITIVE));
	}

	@Override
	public File resolveDirectory(StorageMetadata<? extends T> metadata) {
		try {
			File dir = new File(getRootDirectory(), getDirectoryName(metadata));
			FileUtils.forceMkdir(dir);
			return dir;
		} catch (IOException e) {
			throw new ViennaException(e);
		}
	}

	@Override
	public File resolveFile(StorageMetadata<? extends T> metadata) {
		return new File(resolveDirectory(metadata), getFileName(metadata) + '.' + ext);
	}
}
